package example.simple_mindmap.listener;

import cn.hutool.json.JSONUtil;
import example.simple_mindmap.domain.po.Message;
import example.simple_mindmap.service.MessageServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class MessageListener {
    private final MessageServiceImpl messageService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "message.send.queue"),
            exchange = @Exchange(name = "knowTree.direct"),
            key = "message.send"
    ))
    public void listenPaySuccess(String message){
        try {
            System.out.println(message);
            System.out.println(messageService.sendMessage(JSONUtil.toBean(message, Message.class)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
